/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classesJpa;

import classesBancodeDados.FichaAtendPsiqUrgencia;
import classesJpa.exceptions.NonexistentEntityException;
import classesJpa.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;

/**
 *
 * @author Ives
 */
public class FichaAtendPsiqUrgenciaJpaController implements Serializable {

    public FichaAtendPsiqUrgenciaJpaController() {
        emf = Persistence.createEntityManagerFactory("Juliano_MoreiraPU");
    }

    public FichaAtendPsiqUrgenciaJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FichaAtendPsiqUrgencia fichaAtendPsiqUrgencia) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(fichaAtendPsiqUrgencia);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFichaAtendPsiqUrgencia(fichaAtendPsiqUrgencia.getSus()) != null) {
                throw new PreexistingEntityException("FichaAtendPsiqUrgencia " + fichaAtendPsiqUrgencia + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FichaAtendPsiqUrgencia fichaAtendPsiqUrgencia) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            fichaAtendPsiqUrgencia = em.merge(fichaAtendPsiqUrgencia);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = fichaAtendPsiqUrgencia.getSus();
                if (findFichaAtendPsiqUrgencia(id) == null) {
                    throw new NonexistentEntityException("The fichaAtendPsiqUrgencia with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
public FichaAtendPsiqUrgencia findFicha(String sus, String nPront){
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from FichaAtendPsiqUrgencia as o where sus = '" + sus + "' and nPront = '" + nPront + "'");
            return (FichaAtendPsiqUrgencia)q.getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }finally{
            em.close();
        }
    }
    
    public List<FichaAtendPsiqUrgencia> findFichasPorPaciente(String sus){
        EntityManager em = this.getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from FichaAtendPsiqUrgencia as o where sus = '" + sus + "'");
            return q.getResultList();
        }catch(NoResultException e){
            return null;
        }finally{
            em.close();
        }
    }
    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FichaAtendPsiqUrgencia fichaAtendPsiqUrgencia;
            try {
                fichaAtendPsiqUrgencia = em.getReference(FichaAtendPsiqUrgencia.class, id);
                fichaAtendPsiqUrgencia.getSus();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The fichaAtendPsiqUrgencia with id " + id + " no longer exists.", enfe);
            }
            em.remove(fichaAtendPsiqUrgencia);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FichaAtendPsiqUrgencia> findFichaAtendPsiqUrgenciaEntities() {
        return findFichaAtendPsiqUrgenciaEntities(true, -1, -1);
    }

    public List<FichaAtendPsiqUrgencia> findFichaAtendPsiqUrgenciaEntities(int maxResults, int firstResult) {
        return findFichaAtendPsiqUrgenciaEntities(false, maxResults, firstResult);
    }

    private List<FichaAtendPsiqUrgencia> findFichaAtendPsiqUrgenciaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from FichaAtendPsiqUrgencia as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FichaAtendPsiqUrgencia findFichaAtendPsiqUrgencia(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FichaAtendPsiqUrgencia.class, id);
        } finally {
            em.close();
        }
    }

    public int getFichaAtendPsiqUrgenciaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from FichaAtendPsiqUrgencia as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
